package anonymus.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import anonymus.excepcion.DAOExcepcion;
import anonymus.modelo.Local;
import anonymus.util.ConexionBD;


public class localDAO extends BaseDAO{

	

	public Local obtenerNomLocal(String nombreLoc,String propietario) throws DAOExcepcion {
        System.out.println("localDAO: obtenerNomLocal(String nombreLoc,String propietario)");
        Local vo = null;
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String query = "select codLocal,nombreLoc,direccion,distrito, mapa, telfLoc,usuario_username from Local where nombreLoc=? and usuario_username=?";
            con = ConexionBD.obtenerConexion();
            stmt = con.prepareStatement(query);
            stmt.setString(1, nombreLoc);
            stmt.setString(2, propietario);
            rs = stmt.executeQuery();
            if (rs.next()) {
                vo = new Local();
                vo.setCodLocal(rs.getInt(1));
                vo.setNombreLoc(rs.getString(2));
                vo.setDireccion(rs.getString(3));
                vo.setDistrito(rs.getString(4));
                vo.setMapa(rs.getString(5));
                vo.setTelfLoc(rs.getString(6));
                vo.setPropietario(rs.getString(7));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            throw new DAOExcepcion(e.getMessage());
        } finally {
            this.cerrarResultSet(rs);
            this.cerrarStatement(stmt);
            this.cerrarConexion(con);
        }
        return vo;
    }	
	
	
public void insertar(Local vo) throws DAOExcepcion {
		System.out.println("localDAO: insertar(Local vo)");
		String query = "INSERT INTO Local(nombreLoc, direccion, distrito, mapa, telfLoc, usuario_username) VALUES (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			//stmt.setInt(1, vo.getCodLocal()); el codigo se autogenera
			stmt.setString(1, vo.getNombreLoc());
			stmt.setString(2, vo.getDireccion());
			stmt.setString(3, vo.getDistrito());
			stmt.setString(4, vo.getMapa());
			stmt.setString(5, vo.getTelfLoc());
			stmt.setString(6, vo.getPropietario());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}


	public void eliminar(String codLocal) throws DAOExcepcion {
		System.out.println("localDAO: eliminar(String codLocal)");
		String query = "DELETE FROM Local WHERE codLocal=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, codLocal);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void actualizar(Local vo) throws DAOExcepcion {
		System.out.println("localDAO: actualizar(Local vo)");
		String query = "update Local set(nombreLoc, direccion, distrito, mapa, telfLoc) VALUES (?,?,?,?,?) where codLocal=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombreLoc());
			stmt.setString(2, vo.getDireccion());
			stmt.setString(3, vo.getDistrito());
			stmt.setString(4, vo.getMapa());
			stmt.setString(5, vo.getTelfLoc());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("o se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

        public Collection<Local> Listar() throws DAOExcepcion {
                String query = "SELECT codLocal, nombreLoc FROM Local" ;
                Collection<Local> lista = new ArrayList<Local>();
                Connection con = null;
                PreparedStatement stmt = null;
                ResultSet rs = null;
                try {
                        con = ConexionBD.obtenerConexion();
                        stmt = con.prepareStatement(query);
                        rs = stmt.executeQuery();
                        while (rs.next()) {
                                Local vo = new Local();
                                vo.setCodLocal(rs.getInt("codLocal"));
                                vo.setNombreLoc(rs.getString("nombreLoc"));
                                lista.add(vo);
                        }
                } catch (SQLException e) {
                        System.err.println(e.getMessage());
                        throw new DAOExcepcion(e.getMessage());
                } finally {
                        //this.cerrarResultSet(rs);
                        //this.cerrarStatement(stmt);
                        //this.cerrarConexion(con);
                }
                return lista;
        }
        
        public Collection<Local> ListarDistrito() throws DAOExcepcion {
            String query = "SELECT distrito FROM Local group by distrito" ;
            Collection<Local> lista = new ArrayList<Local>();
            Connection con = null;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try {
                    con = ConexionBD.obtenerConexion();
                    stmt = con.prepareStatement(query);
                    rs = stmt.executeQuery();
                    while (rs.next()) {
                            Local vo = new Local();
                            vo.setDistrito(rs.getString("distrito"));
                            lista.add(vo);
                    }
            } catch (SQLException e) {
                    System.err.println(e.getMessage());
                    throw new DAOExcepcion(e.getMessage());
            } finally {
                    this.cerrarResultSet(rs);
                    this.cerrarStatement(stmt);
                    this.cerrarConexion(con);
            }
            return lista;
    }
}
